//可以统一加载数据
axios.defaults.baseURL = "http://123.57.109.30:3007"
//响应拦截器
//添加请求拦截器
axios.interceptors.request.use(function (config) {
    if (config.url.startsWith("/my")) {
        config.headers['Authorization'] = sessionStorage.getItem("token")
    }
    return config;

}), function (error) {
    return Promise.reject(error);
}


// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    var layer = layui.layer;
    layer.msg(response.data.message);
    if (response.data.status == 1) {
        return Promise.reject(response.data.message);
    }

    return response;
}, function (error) {//http请求失败 
    console.dir(error);
    if (error.response.status == 403) {
        layer.msg(error.response.status.data.message, {
            icon: 2, end: function () {
                window.parent.location.href = "../login.html"
            }

        });
        return Promise.reject(error.response.data.message);
    }



    // 对响应错误做点什么
    return Promise.reject(error);
});






//注册
const registerAPI = (arg, fn) => axios({
    url: "/api/reguser",
    method: "POST",
    data: arg
}).then(fn)//axios请求成功就会调用外部的函数体执行,传参

//登录
const loginAPI = (arg, fn) => axios({
    url: "/api/login",
    method: "POST",
    data: arg

}).then(fn)
//分类新增
const addCateAPI = (arg, fn) => axios({
    url: "/my/article/addcates",
    method: "POST",
    data: arg,
    //请求头

}).then(fn);

//类别管理
const cateListAPI = (fn) => axios({
    url: "/my/article/cates",
    method: "GET",

    //请求头

}).then(fn);

//分类删除
const delCateAPI = (arg, fn) => axios({
    url: "/my/article/deletecate/" + arg.id,
    method: "GET",
    //请求头
    data: arg,



}).then(fn);
//分类编辑
const upCateAPI = (arg, fn) => axios({
    url: "/my/article/deletecate/",
    method: "POST",
    data: arg,

}).then(fn)



//个人中心分类
const getUserInfoAPI = (arg, fn) => axios({
    url: "/my/userinfo",
    method: "GET",

}).then(fn)


//修改分类
const upUserInfoAPI = (arg, fn) => axios({
    url: "/my/userinfo",
    method: "POST",
    data: arg
}).then(fn)
//个人中心-重置密码
const rePassAPI = (arg, fn) => axios({
    url: "/my/updatepwd",
    data: arg,
    method: "POST",
}).then(fn);